unit dmIdocqment;

interface

uses
  SysUtils, Classes, ImgList, Controls, ZConnection,Forms, ExtDlgs, Dialogs,
  DB, ZAbstractRODataset, ZAbstractDataset, ZDataset, frxExportODF,
  frxExportMail, frxExportCSV, frxExportText, frxExportImage,
  frxDCtrl, frxDesgn, frxExportPDF, frxExportRTF,
  frxExportXML, frxExportXLS, frxExportHTML, frxExportTXT,
  frxZOSComponents, frxDBXComponents, frxIBXComponents, frxADOComponents,
  frxClass, frxBDEComponents, frxDBSet, frxGZip, frxDMPExport, frxGradient,
  frxChBox, frxCross, frxRich, frxOLE, frxBarcode, IdBaseComponent,
  IdComponent, IdTCPConnection, IdTCPClient;

type
  Tdm = class(TDataModule)
    ImageList1: TImageList;
    cnDB: TZConnection;
    OpenDialog: TOpenDialog;
    OpenPictureDialog: TOpenPictureDialog;
    qrConfig: TZQuery;
    frxReport: TfrxReport;
    frxBarCodeObject1: TfrxBarCodeObject;
    frxOLEObject1: TfrxOLEObject;
    frxRichObject1: TfrxRichObject;
    frxCrossObject1: TfrxCrossObject;
    frxCheckBoxObject1: TfrxCheckBoxObject;
    frxGradientObject1: TfrxGradientObject;
    frxDotMatrixExport1: TfrxDotMatrixExport;
    frxGZipCompressor1: TfrxGZipCompressor;
    frxDBDataset1: TfrxDBDataset;
    frxBDEComponents1: TfrxBDEComponents;
    frxADOComponents1: TfrxADOComponents;
    frxIBXComponents1: TfrxIBXComponents;
    frxDBXComponents1: TfrxDBXComponents;
    frxZOSComponents1: TfrxZOSComponents;
    frxTXTExport1: TfrxTXTExport;
    frxHTMLExport1: TfrxHTMLExport;
    frxXLSExport1: TfrxXLSExport;
    frxXMLExport1: TfrxXMLExport;
    frxRTFExport1: TfrxRTFExport;
    frxBMPExport1: TfrxBMPExport;
    frxJPEGExport1: TfrxJPEGExport;
    frxTIFFExport1: TfrxTIFFExport;
    frxPDFExport1: TfrxPDFExport;
    frxDesigner1: TfrxDesigner;
    frxDialogControls1: TfrxDialogControls;
    frxGIFExport1: TfrxGIFExport;
    frxSimpleTextExport1: TfrxSimpleTextExport;
    frxCSVExport1: TfrxCSVExport;
    frxMailExport1: TfrxMailExport;
    frxODSExport1: TfrxODSExport;
    frxODTExport1: TfrxODTExport;
    qrLang: TZReadOnlyQuery;
    frxZOSComponents: TfrxZOSComponents;
    qrChk_Right: TZQuery;
    IdTCPClient: TIdTCPClient;
    procedure cnDBBeforeConnect(Sender: TObject);
    procedure DataModuleCreate(Sender: TObject);
    procedure frxReportGetValue(const VarName: String; var Value: Variant);
    procedure DataModuleDestroy(Sender: TObject);
  private
    { Private declarations }
    Fuid: integer;
    Fusers: string;
    Flanguagedef: string;
    Fgroups: string;

  public
    { Public declarations }
    property users:string read Fusers write Fusers;
    property uid:integer read Fuid write Fuid;
    property languagedef:string read Flanguagedef write Flanguagedef;
    property groups:string read Fgroups write Fgroups;
  end;

var
  dm: Tdm;

implementation
uses Inifiles,uEncrypt;

{$R *.dfm}

procedure Tdm.cnDBBeforeConnect(Sender: TObject);
const
 my_key = 33189;
var fPur:TIniFile;
  sDecrypted:AnsiString;
begin
  fPur:=TIniFile.create(ExtractFilePath(Application.ExeName)+'Emborider.ini' );
  try
    with cnDB do
    begin
      if (uppercase(Extractfilename(Application.ExeName))='EMBORIDER.EXE')or
        (uppercase(Extractfilename(Application.ExeName))='EMBORIDERDEMO.EXE')
      then
      begin
        //Database := fPur.ReadString( 'comdb', 'Database', 'IDOCQMENT');
        Database :=ExtractFilePath(Application.ExeName)+'database\EMBOR.DB';
        HostName := fPur.ReadString( 'DB', 'HostName', '127.0.0.1');
        Protocol:='sqlite-3';
        User := '';
        //sDecrypted := Decrypt(fPur.ReadString('DB', 'Password', ''),my_key);
        Password :=''; //sDecrypted;
        Port := strToint(fPur.ReadString( 'DB', 'Port', '3306'));
      end else
      begin
        Database := fPur.ReadString( 'DB', 'Database', 'EMBOR');
        HostName := fPur.ReadString( 'DB', 'HostName', '127.0.0.1');
        Protocol := fPur.ReadString( 'DB', 'Protocol', 'mysql');
        User := fPur.ReadString( 'DB', 'Users', 'root');
        sDecrypted := Decrypt(fPur.ReadString('DB', 'Password', ''),my_key);
        Password := sDecrypted;
        Port := strToint(fPur.ReadString( 'DB', 'Port', '3306'));
      end;
    end;
  finally
    fPur.free;
    fPur:=nil;
  end;
end;

procedure Tdm.DataModuleCreate(Sender: TObject);
begin
  cnDB.Connected:=false;
  qrConfig.open;

  Flanguagedef:=qrConfig.fieldbyname('deflang').asstring;
end;

procedure Tdm.frxReportGetValue(const VarName: String; var Value: Variant);
begin
  if VarName='userprint' then Value:=Fusers;
end;

procedure Tdm.DataModuleDestroy(Sender: TObject);
begin
  if (uppercase(Extractfilename(Application.ExeName))<>'EMBORIDER.EXE')or
    (uppercase(Extractfilename(Application.ExeName))<>'EMBORIDERDEMO.EXE') then
  begin
    qrConfig.open;
    //--check Maximum License;
    with IdTCPClient do
    begin
      try
        try
          Host := qrConfig.fieldbyname('serverip').asstring;
          Port := qrConfig.fieldbyname('port').asinteger;

          Connect;
          Writeln('DE');
        except on e:exception do
          begin
            showmessage('Cannot connect license server ,please check server license is running-->'+e.message);
          end;
        end;
      finally
        Disconnect;
      end;
    end;
  end;
end;

end.
